﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Web;

namespace ZLDC.Common
{
    public class XMLFunction
    {
        private XmlDocument xmlDoc = new XmlDocument();

        public XMLFunction()
        { }

        #region 公共方法

                                public enum enumXmlPathType
        {
            AbsolutePath,
            VirtualPath
        }

                                        public static XmlDocument GetXmlDocument(string xmlFilePath, enumXmlPathType xmlFilePathType)
        {
            XmlDocument xmlDoc = new XmlDocument();
            string xmlFileFullPath = string.Empty;
            if (xmlFilePathType == enumXmlPathType.AbsolutePath)
            {
                xmlFileFullPath = xmlFilePath;
            }
            else if (xmlFilePathType == enumXmlPathType.VirtualPath)
            {
                xmlFileFullPath = HttpContext.Current.Server.MapPath(xmlFilePath);
            }
            xmlDoc.Load(xmlFileFullPath);
            return xmlDoc;
        }

                                                        public static List<string> GetXmlDocumentNode(XmlDocument doc, string nodePath)
        {
            List<string> NodeValues = new List<string>();
            XmlNode nodes = doc.DocumentElement.SelectSingleNode("/" + nodePath);

                        foreach (XmlNode personElement in nodes.ChildNodes)
            {
                NodeValues.Add(personElement.Value);
            }
            return NodeValues;
        }

                                                                public static List<string> GetXmlDocumentNode(XmlDocument doc, string nodePath, string attrName)
        {
            List<string> NodeValues = new List<string>();
            XmlNode nodes = doc.DocumentElement.SelectSingleNode("/" + nodePath);

                        foreach (XmlNode personElement in nodes.ChildNodes)
            {
                NodeValues.Add(personElement.Attributes[attrName].InnerText.Trim());
            }
            return NodeValues;
        }

                                                        public static DataTable GetDataTableByXmlFile(string xmlFilePath, enumXmlPathType xmlFilePathType, string nodeName)
        {
            DataSet ds = new DataSet();
            string xmlFileFullPath = string.Empty;
            if (xmlFilePathType == enumXmlPathType.AbsolutePath)
            {
                xmlFileFullPath = xmlFilePath;
            }
            else if (xmlFilePathType == enumXmlPathType.VirtualPath)
            {
                xmlFileFullPath = HttpContext.Current.Server.MapPath(xmlFilePath);
            }
            ds.ReadXml(xmlFileFullPath);
            return ds.Tables[nodeName];
        }

        #endregion
    }
}
